// pages/smartlife/bus/busline/busline.js
var common = require('../../../../utils/common.js');
var needRefresh = false
Page({
  data: {
    busRouteDetail: {},
    startStationName: '',
    endStationName: '',
    upDown: '',
    lineId: '',
    lineName: '',
    adHidden: true,
    adHeight: 100
  },
  queryMultipleNodes: function () {
    //创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    query.select('.ad-bottom').boundingClientRect(function (rect) {
      console.log(rect.height)
      that.setData({
        adHeight: rect.height
      })
    }).exec();
  },
  onLoad: function (options) {

    console.log(options.lineId + options.lineName + options.upDown + options.startStationName + options.endStationName)
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(lares) {
        console.log(lares)
        wx.setNavigationBarTitle({
          title: options.lineName
        })
        wx.showToast({
          title: '加载中',
          icon: 'loading',
          duration: 10000
        })
        that.setData({
          startStationName: options.startStationName,
          endStationName: options.endStationName,
          upDown: options.upDown,
          lineId: options.lineId,
          lineName: options.lineName,
          latitude: lares.latitude,
          longitude: lares.longitude
        })
        wx.request({
          url: common.trafficUrl() + 'transitSearch/lineGps',
          data: {
            lineId: options.lineId,
            upOrDown: options.upDown,
            latitude: lares.latitude,
            longtitude: lares.longitude
          },
          header: {
            // "Content-Type": "application/x-www-form-urlencoded"
            token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9'
          },
          method: "GET",
          success: function (res) {
            console.log(that.data.longitude)
            console.log(res)
            if (res.data) {
              // setTimeout(function () {
              //   //要延时执行的代码
              //   that.queryMultipleNodes()
              // }, 1000) //延迟时间 这里是1秒
              // var busRouteDetail = res.data.data[0]
              // var runList = res.data.data[0].runList
              // // console.log(runList)
              // var uniqueLunList = that.unique(runList)
              // // console.log(uniqueLunList)
              // busRouteDetail.runList = uniqueLunList
              that.setData({
                busRouteDetail: res.data,
                // adHidden:false
              })
            } else {

            }
            wx.hideToast()
          }
        })
      },
      fail(err) {
        console.log(err)
      }
    })

  },
  onShareAppMessage: function () {
    var that = this
    return {
      title: that.data.lineName + '：' + that.data.startStationName + '→' + that.data.endStationName,
      path: '/pages/smartlife/bus/busline/busline?lineId=' + that.data.lineId + '&lineName=' + that.data.lineName + '&upDown=' + that.data.upDown + '&startStationName=' + that.data.startStationName + '&endStationName=' + that.data.endStationName
    }
  },
  onShow: function () {
    if (needRefresh) {
      this.refresh()
    }
  },
  onHide: function () {
    needRefresh = true
  },
  reverse: function () {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    var upDown = that.data.upDown == 0 ? 1 : 0
    that.setData({
      startStationName: that.data.endStationName,
      endStationName: that.data.startStationName,
      upDown: upDown
    })
    wx.request({
      url: common.trafficUrl() + 'transitSearch/lineGps',
      data: {
        lineId: that.data.lineId,
        upOrDown: that.data.upDown,
        latitude: that.data.latitude,
        longtitude: that.data.longitude
      },
      header: {
        token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9'
      },
      method: "GET",
      success: function (res) {
        console.log(res)
        if (res.data) {
          // var busRouteDetail = res.data.data[0]
          // var runList = res.data.data[0].runList
          // // console.log(runList)
          // var uniqueLunList = that.unique(runList)
          // // console.log(uniqueLunList)
          // busRouteDetail.runList = uniqueLunList
          that.setData({
            busRouteDetail: res.data
          })
        } else {

        }
        wx.hideToast()
      }
    })
  },
  refresh: function () {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    wx.request({
      url: common.trafficUrl() + 'transitSearch/lineGps',
      data: {
        lineId: that.data.lineId,
        upOrDown: that.data.upDown,
        latitude: that.data.latitude,
        longtitude: that.data.longitude
      },
      header: {
        token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9'
      },
      method: "GET",
      success: function (res) {
        console.log(res)
        if (res.data) {
          // var busRouteDetail = res.data.data[0]
          // var runList = res.data.data[0].runList
          // // console.log(runList)
          // var uniqueLunList = that.unique(runList)
          // // console.log(uniqueLunList)
          // busRouteDetail.runList = uniqueLunList
          that.setData({
            busRouteDetail: res.data
          })
        } else {

        }
        wx.hideToast()
      }
    })
  },
  mapClick: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/smartlife/bus/busline/buslinemap/buslinemap?upDown=' + that.data.upDown + '&lineId=' + that.data.lineId + '&lineName=' + that.data.lineName + '&latitude=' + that.data.latitude + '&longitude=' + that.data.longitude
    })
  },
  stationClick: function (e) {
    var that = this
    var lat = that.data.busRouteDetail.relationStations[e.currentTarget.dataset.index].location.latitude
    var lgt = that.data.busRouteDetail.relationStations[e.currentTarget.dataset.index].location.longtitude
    wx.navigateTo({
      url: '/pages/smartlife/bus/busline/buslinemap/buslinemap?upDown=' + that.data.upDown + '&lineId=' + that.data.lineId + '&latitude=' + lat + '&longitude=' + lgt + '&lineName=' + that.data.lineName
    })
  }
})